#!/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
author : DELL
project : ApiTestPlatform
date : 2021/11/22
"""

import pandas as pd
from openpyxl import Workbook
import openpyxl
import os, sys

# https://geek-docs.com/python/python-tutorial/python-openpyxl.html
# book = Workbook()
# sheet = book.active

# module_path = os.path.abspath(os.path.dirname(sys.argv[0]))  # 获得绝对路径
# if os.name == 'nt':
#     testfile = module_path + r'.\.xlsx'
# else:
#     testfile = module_path + './testdata.xlsx'

basedir = os.path.abspath(os.path.dirname(__file__))
testfile = basedir+'users.xlsx'

class ExcelHandle:
    def __init__(self, file=testfile):
        self.file = file

    def read_target(self, target):
        book = openpyxl.load_workbook(self.file)
        sheet = book.active
        return sheet[target].value

    def write_value(self,target,value):
        book = openpyxl.load_workbook(self.file)
        sheet = book.active
        sheet[target] = value
        book.save(self.file)
        print('写入成功!')


    def read_user(self):
        return self.read_excel_target('A2')

    def read_pwd(self):
        return self.read_excel_target('B2')

    # def read_col(self,col):

    # def read_users(self,'users',):

    def write_token(self, token):
        # sheet.cell(row=2, column=3).value = token
        book = openpyxl.load_workbook(self.file)
        sheet = book.active
        sheet['C2'] = token
        book.save(self.file)
        print('写入成功')

    def read_token(self, uid):
        return self.read_target('C2')

    def read_uid(self):
        return self.read_target('D2')

    def read_category(self):
        return self.read_target('G2')

    def write_uid(self, uid):
        book = openpyxl.load_workbook(self.file)
        sheet = book.active
        sheet['D2'] = uid
        book.save(self.file)
        print('写入成功')






if __name__ == '__main__':
    file = r'/testcase/users.xlsx'
    eh = ExcelHandle(file)
    eh.read_excel()
    eh.write_token('11')
